package cn.edu.zut.project1.service.Impl;

import cn.edu.zut.project1.dto.ChangeProductDTO;
import cn.edu.zut.project1.dto.ProductDTO;
import cn.edu.zut.project1.mapper.ProductMapper;
import cn.edu.zut.project1.pojo.Product;
import cn.edu.zut.project1.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service

public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    // 发布商品：将DTO转成实体，关联用户ID

    /**
     * 1.发布商品
     * userId为商品发布时所属的用户
     */
    @Override
    @Transactional
    public void publishProduct(ProductDTO productDTO, Integer userId) {
        Product product = new Product();
        product.setName(productDTO.getName());
        product.setDetail(productDTO.getDetail());
        product.setCoverList(productDTO.getCoverList());
        product.setOldLevel(productDTO.getOldLevel());
        product.setCategoryId(productDTO.getCategoryId());
        product.setUserId(userId);
        product.setInventory(productDTO.getInventory());
        product.setPrice(productDTO.getPrice());
        product.setIsBargain(productDTO.getIsBargain());
        product.setStatus(0);

        int rows = productMapper.insert(product);
        if (rows != 1) {
            throw new RuntimeException("发布商品失败，请重试");
        }

    }

    /**
     * 2.查询我的的商品
     */

    @Override
    public List<Product> getMyProduct(Integer userid) {
        return productMapper.selectByUserId(userid);
    }

    /**
     * 3.查询商品详细信息
     */
    @Override
    public Product getProductDetail(Integer productId) {
        Product product = productMapper.selectById(productId);
        if(product == null){
            throw new RuntimeException("商品不存在或已下架");
        }
        return product;

    }

    /**
     * 4.修改商品状态(只能修改自己的)
     * @param productId
     * @param status
     * @param userId
     */
    @Override
    public void updateProductStatus(Integer productId, Integer status, Integer userId) {
        Product product = productMapper.selectById(productId);
        if(product == null){
            throw new RuntimeException("该商品不存在");
        }
        if(!product.getUserId().equals(userId)){
            throw new RuntimeException("你无权修改他人的商品状态");
        }
        int rows = productMapper.updateStatus(productId,status);
        if(rows != 1){
            throw new RuntimeException("修改商品状态失败");
        }
    }

/**
 * 5.查询所有在售商品
 */
    @Override
    public List<Product> getAllOnSaleProducts() {
        return productMapper.selectAllOnSale();
    }

    // 6. 修改商品信息
    @Override
    public void updateProduct(ChangeProductDTO productDTO, Product product, Integer userId) {
        // 1. 校验商品是否存在
        Product oldProduct = productMapper.selectById(product.getId());
        if (oldProduct == null) {
            throw new RuntimeException("商品不存在");
        }
        // 2. 校验权限：只能修改自己的商品
        if (!oldProduct.getUserId().equals(userId)) {
            throw new RuntimeException("无权修改他人商品");
        }
        // 3. 执行修改
        product.setName(productDTO.getName());
        product.setDetail(productDTO.getDetail());
        product.setOldLevel(productDTO.getOldLevel());
        product.setPrice(productDTO.getPrice());
        product.setIsBargain(productDTO.getIsBargain());
        product.setCategoryId(productDTO.getCategoryId());
        product.setInventory(productDTO.getInventory());
        product.setStatus(0);

        int rows = productMapper.updateProduct(product);
        if (rows != 1) {
            throw new RuntimeException("修改商品失败");
        }
    }

    // 7. 删除商品
    @Override
    public void deleteProduct(Integer productId, Integer userId) {
        int rows = productMapper.deleteProduct(productId, userId);
        if (rows != 1) {
            throw new RuntimeException("删除失败，商品不存在或无权操作");
        }
    }

    // 8. 根据分类ID查询商品
    @Override
    public List<Product> getProductsByCategory(Integer categoryId) {

        return productMapper.selectByCategoryId(categoryId);

    }


}
